import {getLoginStatus, getUserInfo} from '../../common/service/user-service';
import toast from '../../common/service/toast-service';
import {getGiftDetail, openGift} from '../../data-model/model-opengift';
import {eventBusGetter} from '../../utils/eventbus';
import {LOGIN_STATUS, EVENT_BUS_KEY, OPEN_TYPE, PAGE_NAME} from '../../common/constants';
import {routerGetter} from '../../utils/router';

let pageConf = {
    data: {
        uid: '', // 发礼物的uid
        lessonId: '', // 课程id
        giftId: '', // 礼物id
        canOpen: true, // 是否能够打开
        giftImgUrl: '', // 封面照
        receiveDesc: '', // header 右侧描述信息
        courseId: '', // 课程id 直接购买时获取支付状态 发起支付使用
        lessonUri: '', // 小节uri 拆礼物成功后跳转
        courseUri: '', // 课程uri 直接购买成功后跳转
        lessonName: '', // 小节名称
        courseName: '', // 课程名称
        teachertxt: '', // 主讲人
        openStatus: -1, // 是否已经拆过
        users: [], // 已领礼物的人的头像
        isFetch: false // 是否拉取到课程详情
    },
    /**
     * 页面初始化时获取giftId 执行加载礼物详情并填充页面
     * @param options
     */
    onLoad (options) {
        console.log(options);
        this.data.uid = options.uid;
        this.data.lessonId = options.lessonId;
        this.refreshPage.bind(this);
        eventBusGetter().$on(EVENT_BUS_KEY.LOGIN_STATUS_UPDATE, this.refreshPage);
        this.data.uid = options.uid;
        this.data.lessonId = options.lessonId;
    },
    onShow () {
        this.refreshPage();
    },
    onUnload () {
        eventBusGetter().$off(EVENT_BUS_KEY.LOGIN_STATUS_UPDATE, this.refreshPage);
    },
    onClickOpen () {
        let that = this;
        if (this.data.openStatus === 0 && this.data.giftId) {
            // 礼物状态未未拆开
            openGift(this.data.giftId) // 拆礼物
                .then(res => {
                    // 执行router跳转
                    routerGetter().execute(that.data.lessonUri, OPEN_TYPE.REDIRECT, PAGE_NAME.OPEN_GIFT);
                })
                .catch(err => {
                    console.log('拆礼物失败', err);
                    // 领取名额已用尽
                    if (err.statusCode === 813) {
                        if (err.header && err.header.msg) {
                            toast.showLoadingToast(decodeURIComponent(err.header.msg));
                        } else {
                            toast.showLoadingToast('领取名额已用尽');
                        }
                        // 重新刷新礼物详情页面
                        that.refreshPage();
                    }
                });
        } else if (this.data.openStatus > 0) {
            // 礼物状态为已拆开
            // 执行router跳转
            routerGetter().execute(that.data.lessonUri, OPEN_TYPE.REDIRECT, PAGE_NAME.OPEN_GIFT);
        }
    },

    onClickPay () {
        // 跳转至课程详情
        routerGetter().execute(this.data.courseUri, OPEN_TYPE.REDIRECT, PAGE_NAME.OPEN_GIFT);
    },
    /**
     * 获取礼物详情
     * @param giftId
     */
    fetchGift (uid, lessonId) {
        let that = this;
        getGiftDetail(uid, lessonId)
            .then(gift => {
                console.log('拉取礼物详情成功', gift);
                that.setData({
                    giftId: gift.giftId,
                    canOpen: gift.canOpen,
                    giftImgUrl: gift.giftImgUrl,
                    lessonName: gift.lessonName, // 小节名称
                    courseName: gift.courseName,
                    teachertxt: gift.teachertxt,
                    users: gift.users,
                    lessonUri: gift.lessonUri,
                    courseUri: gift.courseUri,
                    courseId: gift.courseId,
                    openStatus: gift.openStatus,
                    receiveDesc: gift.receiveDesc,
                    isFetch: true
                });
                // 如果礼物状态为已拆开 直接跳转
                if (gift.openStatus > 0) {
                    routerGetter().execute(gift.lessonUri, OPEN_TYPE.REDIRECT, PAGE_NAME.OPEN_GIFT);
                }
                // 如果礼物已被抢完, 且当前用户是发礼物的用户 直接跳转到课程详情页
                if (gift.canOpen === 0) {
                    if (getUserInfo() && getUserInfo().userId === that.data.uid) {
                        routerGetter().execute(gift.courseUri, OPEN_TYPE.REDIRECT, PAGE_NAME.OPEN_GIFT);
                    }
                }
            })
            .catch(err => {
                console.log('获取礼物详情失败', err);
            });
    },
    refreshPage () {
        let that = this;
        if (!that.data.uid && !that.data.lessonId) {
            return;
        }
        if (getLoginStatus() === LOGIN_STATUS.ZH_LOGIN_SUCCESS || getLoginStatus() === LOGIN_STATUS.ZH_LOGIN_FAILED) {
            console.log('登录成功,重新拉取礼物详情');
            that.fetchGift(that.data.uid, that.data.lessonId);
        }
    }
};
Page(pageConf);
